package com.caiwangdai.app.ui.me

import android.content.Context
import android.content.Intent
import com.caiwangdai.app.R
import com.caiwangdai.app.backend.RetrofitHome
import com.caiwangdai.app.backend.observer.OnDataV2Listener
import com.caiwangdai.app.backend.rx.RxSchedulers
import com.caiwangdai.app.bean.UserBankB
import com.caiwangdai.app.bean.base.NormalResponseB
import com.caiwangdai.app.origin.BaseActivity
import com.caiwangdai.app.origin.ToolBarManager
import com.caiwangdai.app.widget.MultiplyStateViewWM
import kotlinx.android.synthetic.main.activity_bank_info.*
import me.grantland.widget.AutofitHelper

//银行信息
class BankInfoActivity : BaseActivity() {
    override fun getLayoutResID() = R.layout.activity_bank_info

    companion object {
        fun newInstance(context: Context): Intent {
            val intent = Intent(context, BankInfoActivity::class.java)
            return intent
        }
    }


    override fun initView() {
        ToolBarManager.with(this, WMContent)
                .setTitle("银行卡")
                .setNavigationIcon(R.drawable.ic_nav_arrow_white)

        AutofitHelper.create(tv_bank_number)

        getUserIdentityInfo()
    }


    private fun getUserIdentityInfo() {
        RetrofitHome.getUserApi()
                .getUserBankInfo()
                .compose(RxSchedulers.toMain())
                .subscribe(object : OnDataV2Listener<UserBankB>(this, swipe_refresh, true) {

                    override fun onSuccess(value: NormalResponseB<UserBankB>) {
                        if (value.data?.info?.isBank == 0) {
                            multiView.setViewState(MultiplyStateViewWM.VIEW_STATE_EMPTY)
                            return
                        }
                        val data = value.data?.info
                        val name = data?.bank_name
                        val number = data?.bank_card

                        tv_bank_name.text = name
                        tv_bank_number.text = reConstructBankNumber(number!!)
                    }
                })
    }

    //处理银行卡号
    fun reConstructBankNumber(bankNumber: String): String {
        var result = ""
        for ((index, str) in bankNumber.withIndex()) {
            result += str
            if (index == 0 || index == bankNumber.length - 1)
                continue
            if ((index + 1) % 4 == 0) {
                result += "    "
            }
        }
        return result
    }
}
